package com.csf.controller.interceptor;

import com.csf.entity.SessionReplace;
import com.csf.entity.User;
import com.csf.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MessageInterceptor implements HandlerInterceptor {

    @Autowired
    private SessionReplace sessionReplace;

    @Autowired
    private MessageService messageService;
//进入视图层之前，获取当前用户并且获取当前用户所有未读消息和通知
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        User user = sessionReplace.getUser();
        if (modelAndView != null && user!=null){
            int unReadCount = messageService.findUnReadCount(user.getId(), null);
            int userLetterUnRead = messageService.userLetterUnRead(user.getId(), null);
            modelAndView.addObject("notReadCount",unReadCount+userLetterUnRead);
        }
    }
}
